<template>
  <div>
    <el-form ref="myform" :model="formItem" label-width="100px" size="mini" class="form-view">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="客户满意度">
            {{ getSatisfactionName(formItem.satisfaction) }}
          </el-form-item>
        </el-col>
        <el-row :gutter="40">
          <el-col :span="24">
            <el-form-item label="附件">
              <el-button v-for="tag in formItem.annex" :key="tag.name" size="mini" type="text" @click.native="downloadFile(tag)">{{ tag.name }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-row></el-form>
  </div>
</template>

<script>
import { customerReviewDetail as detail } from '@neu/dcom-ui/api/alter'
import { customerReviewDownloadFile as downloadFile } from '@neu/dcom-ui/api/alter'
// import { findByDictionaryType } from '@neu/dcom-ui/api/dictionary'
import { getToken } from '@neu/biz-core/utils/auth'

const formItem = {
  // 工作流TaskId
  taskId: '',
  // 工单号
  workNum: '',
  // 客户满意度
  satisfaction: '',
  // 附件列表
  annex: []
}

export default {
  name: 'CustomerReviewViewForm',
  components: {
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 满意度列表
      satisfactionList: [
        { text: '非常不满意', code: 'veryDissatisfied' },
        { text: '不满意', code: 'dissatisfied' },
        { text: '基本满意', code: 'basicallySatisfied' },
        { text: '满意', code: 'satisfied' },
        { text: '非常满意', code: 'verySatisfied' }
      ]
    }
  },
  created() {
    this.fetch()
    // 客户满意度
    // findByDictionaryType('satisfaction').then(response => {
    //   this.satisfactionList = response.data
    // })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    /** 下载附件 */
    downloadFile(tag) {
      downloadFile(tag.fsId).then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = tag.name
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    getSatisfactionName(value) {
      const one = this.satisfactionList.find(e => e.code === value)
      return one ? one.text : ''
    }
  }
}
</script>
